<?php

	/**
	 * @see Bike_Js_Base
	 */
	require_once 'Bike/Js/Base.php';
	
	/**
	 * @see Bike_Js_Interface_String
	 */
	require_once 'Bike/Js/Interface/String.php';
	
	
	class Bike_Js_ID extends Bike_Js_Base implements Bike_Js_Interface_String
	{
		const CLASS_NAME_CACHE_DELIM = '.';
		
		static protected $_defaultSeparator = '-';
        static protected $_defaultPrefix = '';
        static protected $_useCaching = True;
        
        static protected $_varsCache = array();
        
        protected $_separator;
        protected $_prefix;
        protected $_cacheNameStr;

        static public function getDefaultSeparator()
        {        	return self::$_defaultSeparator;
        }

        static public function setDefaultSeparator($Separator)
        {
        	self::$_defaultSeparator = $Separator;
        }

        static public function getDefaultPrefix()
        {
        	return self::$_defaultPrefix;
        }

        static public function setDefaultPrefix($Prefix)
        {
        	self::$_defaultPrefix = $Prefix;
        }
        
        static public function getUseCaching()
        {
        	return self::$_useCaching;
        }
        
        static public function setUseCaching($caching)
        {
        	self::$_useCaching = $caching;
        }
        
        static public function getVarsCache($name=null)
        {
        	if(is_null($name))
        	{
        		return (array) self::$_varsCache;
        	}
        	elseif(isset(self::$_varsCache[$name]))
        	{
        		return self::$_varsCache[$name];
        	}
        	 
        	require_once 'Bike/Js/Exception.php';
        	throw new Bike_Js_Exception(sprintf('There are no cache for "%s"', $name));
        }
        
        static protected function setVarsCache($name, $value)
        {
        	self::$_varsCache[$name] = $value;
        }
        
        public function getSeparator()
        {
        	return (is_null($this->_separator)) ? $this->getDefaultSeparator() : $this->_separator;
        }
        
		public function setSeparator($Separator)
        {
        	$this->_separator = $Separator;
        	return $this;
        }
        
        public function getPrefix()
        {
        	return (is_null($this->_prefix)) ? $this->getDefaultPrefix() : $this->_prefix;
        }
        
        public function setPrefix($Prefix)
        {
        	$this->_prefix = $Prefix;
        	return $this;
        }
        
        protected function setCacheNameStr()
        {
        	$this->_cacheNameStr = join(self::CLASS_NAME_CACHE_DELIM, $this->_getSettings());
        }
        
        protected function getCacheNameStr()
        {
        	return $this->_cacheNameStr;
        }
        
        public function getClassNameCacheStr($name=null)
        {
        	$parts = array();
        	if($this->getCacheNameStr())
        	{
        		array_push($parts, $this->getCacheNameStr());
        	}
        	if(!is_null($name))
        	{
        		array_push($parts, $name);
        	}
        	return join(self::CLASS_NAME_CACHE_DELIM, $parts);
        }

        public function makeOptions(array $Args=null)
		{
			$options = $Args;
			return $options;
		}

		public function __get($Name)
		{			if($this->getUseCaching())
			{
				$classNamePartStr = $this->getClassNameCacheStr($Name);
				try
				{
					return $this->getVarsCache($classNamePartStr);
				}
				catch(Bike_Js_Exception $e)
				{
					try
					{
						$classNamePartPrevStr = $this->getClassNameCacheStr();
						$Object = clone $this->getVarsCache($classNamePartPrevStr);
					}
					catch(Bike_Js_Exception $e)
					{
						$Object = $this;
						if(!is_array($Object->_settings))
						{
							$Object->_settings = array();
						}
					}
					$Object->addSetting($Name);
					$this->setVarsCache($classNamePartStr, $Object);
					return $Object;
				}
			}
			else
			{	
				if(!is_array($this->_settings))
				{
					$this->_settings = array();
				}
				$this->addSetting($Name);
				return $this;
			}
		}
		
		protected function addSetting($setting)
		{
			array_push($this->_settings, $setting);
			$this->setCacheNameStr();
			return $this;
		}
		
		protected function _getSettings()
		{
			return $this->_settings;
		}
		
		public function getID($settings = array())
		{
			$parts = (array) $settings;
			if($this->getPrefix())
			{
				$parts = array_merge((array) $this->getPrefix(), (array) $parts);
			}
			return join($this->getSeparator(), $parts);
		}
		
		public function _getID()
		{
			return $this->getID($this->_getSettings());
		}

		public function _toString()
		{
			//return $this->_getID();
			return $this->_prepareValue($this->_getID());
		}
	}
